package top.korori.service.impl;

import cn.hutool.extra.servlet.ServletUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.korori.common.model.PageData;
import top.korori.common.utils.IpUtils;
import top.korori.entity.SysLoginLog;
import top.korori.entity.bo.SysLoginLogBo;
import top.korori.entity.vo.SysLoginLogVo;
import top.korori.mapper.SysLoginLogMapper;
import top.korori.service.SysLoginLogService;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * 登录日志 服务实现类
 * 
 *
 * @author korori
 * @since
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {

    @Autowired
    private HttpServletRequest request;

    @Override
    public void add(SysLoginLog sysLoginLog) {
        UserAgent userAgent = IpUtils.getUserAgent(request);
        String clientIP = ServletUtil.getClientIP(request);
        sysLoginLog.setOs(userAgent.getOperatingSystem().getName());
        sysLoginLog.setBrowse(userAgent.getBrowser().getName());
        sysLoginLog.setLoginIp(clientIP);
        save(sysLoginLog);
    }

    @Override
    public PageData<SysLoginLogVo> list(SysLoginLogBo sysLoginLogBo) {
        Page<SysLoginLogVo> page = Page.of(sysLoginLogBo.getPageNum(), sysLoginLogBo.getPageSize());
        Page<SysLoginLogVo> sysLoginLogVoPage = baseMapper.list(page,sysLoginLogBo);
        PageData<SysLoginLogVo> pageData = new PageData<>();
        pageData.setTotal(sysLoginLogVoPage.getTotal());
        pageData.setList(sysLoginLogVoPage.getRecords());
        return pageData;
    }

    @Override
    public SysLoginLogVo get(Long loginLogId) {
        return baseMapper.getById(loginLogId);
    }
}
